//
//  UIBarButtonItem+Estension.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/1.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit


extension UIBarButtonItem {
    
    ///  根据文字，图片创建NavgationItem按钮
    ///
    ///  - parameter imageName: 图片名字
    ///  - parameter title:     文字名字
    ///  - parameter target:    谁调用
    ///  - parameter action:    调用方法
    ///
    ///  - returns: 返回UIBarbuttonItem构造好的对象
    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        
        self.init()
        
        let btn = UIButton()
        
        // let 判断是否有名字和title
        
        if let i = imageName {
            btn.setImage(UIImage(named: i), forState: .Normal)
            btn.setImage(UIImage(named: "\(i)_highlighted"), forState: .Highlighted)
  
        }
        if let t = title {
            
            btn.setTitle(t, forState: .Normal)
            btn.setTitle(t, forState: .Highlighted)
            // 字体
            btn.titleLabel?.font = UIFont.systemFontOfSize(14)
            
            // 文字颜色
            btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            btn.setTitleColor(UIColor(colorLiteralRed: 52/255, green: 52/255, blue: 52/255, alpha: 1), forState: .Normal) 
            
        }
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        // 没有显示btn是因为没有设置frame
        btn.sizeToFit()
        
        customView = btn
        
    }
    
    
    
    
    
}





